package com.cn.goodsadmin.controller;


import com.cn.goodsadmin.po.Task;
import com.cn.goodsadmin.service.WaterService;
import com.cn.goodsadmin.util.ErrorMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@CrossOrigin(origins = {"http://10.10.10.143:8020"}, maxAge = 3600)
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
public class WaterController {

    @Autowired
    WaterService waterService;

    @LoginRequired
    @RequestMapping(value = "/addwatertask", method = RequestMethod.POST, produces = "application/json;charset=utf-8", consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap addWaterTask(@RequestBody Task task) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            waterService.saveTask(task);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/deletewatertask", method = RequestMethod.POST, produces = "application/json;charset=utf-8", consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap deleteWaterTask(@RequestBody Task task) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            waterService.deleteTask(task);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/updatewatertask", method = RequestMethod.POST, produces = "application/json;charset=utf-8", consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap updateWaterTask(@RequestBody Task task) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            waterService.updateTask(task);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/findwatertask", method = RequestMethod.GET,params = {"appid"})
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap findWaterTask(int appid) {
        SupplyMap supplyMap = new SupplyMap();
        try {
          List<Task> tasks= waterService.findAllTask(appid);
          if(tasks!=null){
              supplyMap.put("tasks",tasks);
          }
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }


}
